#!/bin/bash

# 修复 Nginx 配置的脚本

echo "========================================="
echo "修复 Nginx 配置"
echo "========================================="
echo ""

SERVER_IP="172.16.29.198"

echo "【1】上传正确的 Nginx 配置..."
scp nginx.conf root@$SERVER_IP:/tmp/paike-nginx.conf

echo ""
echo "【2】在服务器上应用配置..."
ssh root@$SERVER_IP << 'ENDSSH'
# 备份旧配置
cp /etc/nginx/conf.d/paike.conf /etc/nginx/conf.d/paike.conf.bak.$(date +%Y%m%d%H%M%S)

# 应用新配置
cp /tmp/paike-nginx.conf /etc/nginx/conf.d/paike.conf

# 显示配置内容
echo "========================================="
echo "当前 Nginx 配置:"
echo "========================================="
cat /etc/nginx/conf.d/paike.conf
echo ""

# 测试配置
echo "========================================="
echo "测试 Nginx 配置..."
echo "========================================="
nginx -t

# 重新加载 Nginx
if [ $? -eq 0 ]; then
    echo ""
    echo "配置正确，重新加载 Nginx..."
    systemctl reload nginx
    systemctl status nginx | grep Active
    echo ""
    echo "✅ Nginx 配置已更新并重新加载"
else
    echo ""
    echo "❌ Nginx 配置有错误，已回滚"
    cp /etc/nginx/conf.d/paike.conf.bak.* /etc/nginx/conf.d/paike.conf
fi

# 清理临时文件
rm -f /tmp/paike-nginx.conf
ENDSSH

echo ""
echo "========================================="
echo "【测试】现在测试登录功能..."
echo "========================================="
echo ""

echo "测试管理员登录（通过 Nginx）："
curl -X POST "http://$SERVER_IP/api/admin/login" \
  -H "Content-Type: application/json" \
  -d '{"adminNo":"admin","password":"123456"}'

echo ""
echo ""
echo "========================================="
echo "✅ 完成！请在浏览器中访问："
echo "http://$SERVER_IP"
echo "========================================="








